<#include "/layout/header.ftl"/>
<div class="clearfix"></div>
<div class="row">
    <div class="col-md-12 col-sm-12 col-xs-12">
        <ol class="breadcrumb">
            <li><a href="/">首页</a></li>
            <li class="active">学年管理	</li>
        </ol>
        <div class="x_panel">
            <div class="x_content">
                <div class="<#--table-responsive-->">
                    <div class="btn-group hidden-xs" id="toolbar">
                        <@shiro.hasPermission name="term:add">
                        <button id="btn_add" type="button" class="btn btn-default" title="新增学年">
                            <i class="fa fa-plus"></i> 新增学年
                        </button>
                        </@shiro.hasPermission>
                        <@shiro.hasPermission name="term:batchDelete">
                            <button id="btn_delete_ids" type="button" class="btn btn-default" title="删除选中">
                                <i class="fa fa-trash-o"></i> 批量删除
                            </button>
                        </@shiro.hasPermission>
                    </div>
                    <table id="tablelist">
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<#include "/layout/footer.ftl"/>
<!--/弹框-->
<!--添加角色弹框-->
<div class="modal fade" id="addOrUpdateModal" tabindex="-1" role="dialog" aria-labelledby="addroleLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="addroleLabel">添加学年</h4>
            </div>
            <div class="modal-body">
                <form id="addOrUpdateForm" class="form-horizontal form-label-left" novalidate>
                    <input type="hidden" name="id">
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="termName">学年名称: <span class="required">*</span></label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <input type="text" class="form-control col-md-7 col-xs-12" name="termName" id="termName" required="required" placeholder="请输学年名称"/>
                        </div>
                    </div>
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="termStatus">是否启用: <span class="required">*</span></label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <select name="termStatus" id="termStatus" required="required" class="form-control col-md-7 col-xs-12">
                                <option value="">请选择</option>
                                <option value="0">禁用</option>
                                <option value="1">启用</option>
                            </select>
                        </div>
                    </div>
                    <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="termAim">学年目标: <span class="required">*</span></label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <input type="text" class="form-control col-md-7 col-xs-12" name="termAim" id="termAim" placeholder="请输入学年目标"/>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary addOrUpdateBtn">保存</button>
            </div>
        </div>
    </div>
</div>
<!--/添加角色弹框-->
<script>
    /**
     * 操作按钮
     * @param code
     * @param row
     * @param index
     * @returns {string}
     */
    function operateFormatter(code, row, index) {
        var trId = row.id;
        var operateBtn = [
            '<@shiro.hasPermission name="term:edit"><a class="btn btn-xs btn-primary btn-update" data-id="' + trId + '"><i class="fa fa-edit"></i>编辑</a></@shiro.hasPermission>',
            '<@shiro.hasPermission name="term:delete"><a class="btn btn-xs btn-danger btn-remove" data-id="' + trId + '"><i class="fa fa-trash-o"></i>删除</a></@shiro.hasPermission>',
        ];
        if (row.termStatus == 1) {
            operateBtn.push('<@shiro.hasPermission name="term:open"><a class="btn btn-xs btn-primary btn-close" data-id="' + trId + '"><i class="fa fa-unlink"></i>禁用</a></@shiro.hasPermission>');
        }else{
            operateBtn.push('<@shiro.hasPermission name="term:open"><a class="btn btn-xs btn-primary btn-open" data-id="' + trId + '"><i class="fa fa-undo"></i>启用</a></@shiro.hasPermission>')
        }
        return operateBtn.join('');
    }

    $(function () {
        var options = {
            url: "/term/list",
            getInfoUrl: "/term/get/{id}",
            updateUrl: "/term/edit",
            removeUrl: "/term/remove",
            createUrl: "/term/add",
            openUrl: "/term/open",
            columns: [{
                checkbox: true
            }, {
                field: 'termName',
                title: '学年名称',
                editable: true,
            }, {
                field: 'termStatus',
                title: '是否启用',
                editable: false,
                formatter: function (code) {
                    return code == 1 ? '启用' : '禁用';
                }
            }, {
                field: 'termAim',
                title: '学年目标',
                editable: true
            }, {
                field: 'operate',
                title: '操作',
                formatter: operateFormatter //自定义方法，添加操作按钮
            }],
            modalName: "学年"
        };
        //1.初始化Table
        $.tableUtil.init(options);
        //2.初始化Button的点击事件
        $.buttonUtil.init(options);

        /* 分配资源权限 */
        $('#tablelist').on('click', '.btn-close', function () {
            var $this = $(this);
            var id = $this.attr("data-id");
            $.ajax({
                async: false,
                type: "POST",
                data: {'id': id,'status':0},
                url: '/term/open',
                success: function (json) {
                    $.tool.ajaxSuccess(json);
                    $.tableUtil.refresh();
                },
                error: $.tool.ajaxError
            });
        });

        $('#tablelist').on('click', '.btn-open', function () {
            var $this = $(this);
            var id = $this.attr("data-id");
            $.ajax({
                async: false,
                type: "POST",
                data: {'id': id,'status':1},
                url: '/term/open',
                success: function (json) {
                    $.tool.ajaxSuccess(json);
                    $.tableUtil.refresh();
                },
                error: $.tool.ajaxError
            });
        });
    });
</script>